using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Lab3Reflections
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager m_graphics;

        private Matrix World;
        private Matrix View;
        private Matrix Projection;

        private Effect Effect;
        private Model SphereModel;
        private TextureCube SphereTexture;

        private double m_fov = 70.0;
        private Vector3 m_cameraPosition = new Vector3(0, 0, -10);
        private float m_angle = 0;

        public Game1()
        {
            m_graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Load.
            Effect = Content.Load<Effect>("Effects/Effect1");
            SphereModel = Content.Load<Model>("Models/Sphere");
            SphereTexture = Content.Load<TextureCube>("Textures/CubeMap"); 

            // Remap.
            RemapModel(SphereModel, Effect);

            // Set Techniques.
            Effect.CurrentTechnique = Effect.Techniques["Technique1"];
        }

        private static void RemapModel(Model a_model, Effect a_effect)
        {
            foreach (ModelMesh mesh in a_model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = a_effect;
                }
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="a_gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime a_gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
                this.Exit();

            m_angle += 0.002f;
            m_cameraPosition = 20 * new Vector3((float)Math.Sin(m_angle), 0, (float)Math.Cos(m_angle));
            View = Matrix.CreateLookAt(m_cameraPosition, new Vector3(0, 0, 0), Vector3.UnitY);

            base.Update(a_gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="a_gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime a_gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            DrawModel(a_gameTime);

            base.Draw(a_gameTime);
        }

        private void DrawModel(GameTime a_gameTime)
        {
            World = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f) * Matrix.CreateScale(3.0f);
            Projection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI * m_fov / 180.0), GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);

            Effect.Parameters["World"].SetValue(World);
            Effect.Parameters["View"].SetValue(View);
            Effect.Parameters["Projection"].SetValue(Projection);
            Effect.Parameters["ModelTexture"].SetValue(SphereTexture);
            Effect.Parameters["CameraPosition"].SetValue(m_cameraPosition);

            DrawMesh(SphereModel);
        }

        private void DrawMesh(Model a_model)
        {
            foreach (ModelMesh mesh in a_model.Meshes)
            {
                foreach (Effect e in mesh.Effects)
                {
                    mesh.Draw();
                }
            }
        }
    }
}
